//
//  ZYStarMoneyRechargeController.m
//  BestLearning
//
//  Created by Ethan on 2017/9/12.
//  Copyright © 2017年 apple. All rights reserved.
//
// 颜色相关
#define HUDBackgroundColor UIColorWithRGBA(41, 51, 63, 0.6)  //0.6

#define UIColorWithRGBA(r, g, b, a) [[UIColor alloc] initWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
//商户号
#define WXPartnerid @"1487426882"
//微信appId
#define WXAppID @"wx0d900f4f5ca08199"
//AppSecret
#define apiKey @"9176c1ff184d82587d8f2517c3a05a9c"
#import <AlipaySDK/AlipaySDK.h>


#import "WXApi.h"
#import <CommonCrypto/CommonDigest.h>
static NSString *ID = @"LLCourseApplyViewController";
static UIWindow *__sheetWindow = nil;



#import "ZYStarMoneyRechargeController.h"
#import "RechargeRecordVC.h"
#import "ZYStarchongView.h"



@interface ZYStarMoneyRechargeController ()<UITextFieldDelegate>
    {
        double _coins;  //当前用户的星道币
        double _coinsReplace;   //抵扣的星道币数量
        double _payFee;
        double _coninsNew;//新折扣的星道币
        
        
        //微信支付参数
        NSString *_prepayid; //服务器生成的预付单号
        NSString *_noncestr;
        NSString *_sign;
        NSString *_timestamp;
    }
    @property (nonatomic,strong) UIImageView *photoImageView;
    @property (nonatomic,strong) UILabel *nameLabel;
    @property (nonatomic,strong) UILabel *sumLabel;
    @property (nonatomic,strong) UIButton *oneButton;
    @property (nonatomic,strong) UIButton *towButton;
    @property (nonatomic,strong) UIButton *threeButton;
    @property (nonatomic,strong) UIButton *fourButton;
    @property (nonatomic,strong) UIButton *fiveButton;
    @property (nonatomic,strong) UIButton *sixButton;
    @property (nonatomic,strong) UIButton *sevenbutton;
    @property (nonatomic,strong) UIButton *eightButton;
    
    
    @property (nonatomic,strong) UITextField *sevenField;
    @property (nonatomic,strong) UILabel *otherLabel;//其他金额显示
    
    
    @property (nonatomic,strong) UILabel *rbmOne;
    @property (nonatomic,strong) UILabel *rbmTow;
    @property (nonatomic,strong) UILabel *rbmThree;
    @property (nonatomic,strong) UILabel *rbmFour;
    @property (nonatomic,strong) UILabel *rbmFive;
    @property (nonatomic,strong) UILabel *rbmSix;
    
    @property (nonatomic,strong) UILabel *starOne;
    @property (nonatomic,strong) UILabel *starTow;
    @property (nonatomic,strong) UILabel *starThree;
    @property (nonatomic,strong) UILabel *starFour;
    @property (nonatomic,strong) UILabel *starFive;
    @property (nonatomic,strong) UILabel *starSix;
    
    @property (nonatomic,strong) UIWindow *seenWindow;
    
    @property (nonatomic,copy)NSString *rechargeRate;
    @property (nonatomic,strong) UIView *allTowView;
    
    
    
    @property (nonatomic,copy)NSString *boby;//信息
    @property (nonatomic,copy)NSString *subject;//订单的标题
    @property (nonatomic,copy)NSString *total_fee;//订单的价格
    @property (nonatomic,copy)NSString *coinss;//星币抵扣数
    @property (nonatomic,copy)NSString *typeStr;
    @property (nonatomic,copy)NSString *sumPrice;
    @property (nonatomic,copy)NSString *starSum;
    @property (nonatomic,copy)NSString *couponId;//充值模板ID
    @property (nonatomic,strong) NSMutableArray *dataSource;
    @property (nonatomic,strong) NSDictionary *dict;
    

    @end

@implementation ZYStarMoneyRechargeController
    
    
- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
    
}
    
    
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"星币充值";
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]  initWithTitle:@"充值记录" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightItemAction)];
    self.navigationItem.rightBarButtonItem = rightItem;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(WXCallback:) name:WXCallbackNotification object:nil];
    [self setNetWork];
    
    
    
}
    
    
    
- (void)setNetWork{
    
    NSString *url = [NSString stringWithFormat:@"user.do?action=getMyInfoAndRecharge"];
    
    NSLog(@"%@  %@",LLBaseURLs(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            self.dict = [Obj valueForKey:@"t"];
            
            self.dataSource = [self.dict valueForKey:@"rechargeTemplates"];
            
            
            [self setUpSuperView];
            
            
            
            
            
        } else {
            
              [MBProgressHUD showError:@"链接超时"];
        }
        
    } Fail:^(NSError *Error) {
        
    }];
    
  
    
    
}
    
    
- (void)setUpSuperView{
    
    self.photoImageView = [UIImageView new];
    self.photoImageView.image = [UIImage imageNamed:@"xingbi_bg_fefault"];
    self.photoImageView.layer.cornerRadius = 25;
    self.photoImageView.clipsToBounds = YES;
    [self.view addSubview:self.photoImageView];
    
    self.nameLabel = [UILabel new];
    self.nameLabel.font = [UIFont systemFontOfSize:18];
    self.nameLabel.textColor = ColorRGB(51, 51, 51);
    [self.view addSubview:self.nameLabel];
    
    self.sumLabel = [UILabel new];
    self.sumLabel.font = [UIFont systemFontOfSize:11];
    self.sumLabel.textColor = ColorRGB(102, 102, 102);
    [self.view addSubview:self.sumLabel];
    
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.top.offset(10);
        make.width.height.offset(50);
        
    }];
    
    self.nameLabel.text = @"张宇";
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.photoImageView.mas_right).offset(5);
        make.top.offset(15);
        make.height.offset(15);
    }];
    
    self.sumLabel.text = @"余额：25星币";
    [self.sumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.photoImageView.mas_right).offset(5);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        make.height.offset(15);
    }];
    
    UILabel *grayLabel = [UILabel new];
    grayLabel.backgroundColor = ColorRGB(204, 204, 204);
    [self.view addSubview:grayLabel];
    
    [grayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.equalTo(self.photoImageView.mas_bottom).offset(5);
        make.width.offset(kScreenWidth);
        make.height.offset(1);
    }];
    
    UILabel *selectLabel = [UILabel new];
    selectLabel.text = @"请选择充值星币";
    selectLabel.font = [UIFont systemFontOfSize:16];
    selectLabel.textColor = ColorRGB(51, 51, 51);
    [self.view addSubview:selectLabel];
    
    [selectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.photoImageView);
        make.top.equalTo(grayLabel.mas_bottom).offset(15);
    }];
    
    
    
    
    self.nameLabel.text = [NSString stringWithFormat:@"%@", [self.dict valueForKey:@"username"]];
    self.sumLabel.text = [NSString stringWithFormat:@"余额：%.2f星币",[[self.dict valueForKey:@"money"]  doubleValue]];
    
    BOOL isImage = [NSString isBlankString:[self.dict valueForKey:@"avatar"]];
    
    if (isImage) {
        self.photoImageView.image = [UIImage imageNamed:@"xingbi_bg_fefault"];
    }else{
        [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:[self.dict valueForKey:@"avatar"]] placeholderImage:[UIImage imageNamed:@"xingbi_bg_fefault"]];
    }
    
    
    BOOL  isUp =  [NSString isBlankString:[NSString stringWithFormat:@"%@",[self.dict valueForKey:@"rechargeRate"]]];
    if (isUp) {
        self.rechargeRate = @"1";
    }else{
        self.rechargeRate = [NSString stringWithFormat:@"%@",[self.dict valueForKey:@"rechargeRate"]];
    }
    
    
    
    [self setUpTowSuperView];
    
    
    
}
    
- (void)setUpTowSuperView{
    
    self.towButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.towButton.frame = CGRectMake(kScreenWidth/2 -(kScreenWidth/3 -10)/2,130,kScreenWidth/3 -10, 60);
    self.towButton.layer.borderColor = ColorRGB(153, 153, 153).CGColor;
    self.towButton.layer.borderWidth = 1;
    self.towButton.backgroundColor = [UIColor whiteColor];
    self.towButton.layer.cornerRadius = 5;
    self.towButton.clipsToBounds = YES;
    self.towButton.tag = 202;
    [self.view addSubview:self.towButton];
    
    UILabel *towLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.towButton.frame.size.width, self.towButton.frame.size.height/2)];
    [self.towButton addSubview:towLabel];
    towLabel.textAlignment = NSTextAlignmentCenter;
    towLabel.font = [UIFont systemFontOfSize:15];
    towLabel.textColor = ColorRGB(102, 102, 102);
    
    
    
    towLabel.text = [NSString stringWithFormat:@"%.2f元",[[self.dataSource[1] valueForKey:@"paid"] doubleValue]];
    self.rbmTow = towLabel;
    
    UILabel *rmbLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, towLabel.frame.origin.y + towLabel.frame.size.height, towLabel.frame.size.width, towLabel.frame.size.height)];
    rmbLabel.text = [NSString stringWithFormat:@"到账: %.2f星币",[[self.dataSource[1] valueForKey:@"coins"] doubleValue]];
    
    rmbLabel.textColor = [UIColor whiteColor];
    rmbLabel.font = [UIFont systemFontOfSize:11];
    rmbLabel.textColor= ColorRGB(153, 153, 153);
    rmbLabel.textAlignment = NSTextAlignmentCenter;
    self.starTow = rmbLabel;
    [self.towButton addSubview:rmbLabel];
    
    
    
    
    
    
    
    
    
    
    
    self.oneButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.oneButton.frame = CGRectMake(kScreenWidth/2 -(kScreenWidth/3 -10)/2 - (kScreenWidth/3 -10) - 5 ,self.towButton.frame.origin.y, self.towButton.frame.size.width, self.towButton.frame.size.height);
    self.oneButton.layer.borderColor = ColorRGB(153, 153, 153).CGColor;
    self.oneButton.layer.borderWidth = 1;
    self.oneButton.backgroundColor = [UIColor whiteColor];
    self.oneButton.layer.cornerRadius = 5;
    self.oneButton.clipsToBounds = YES;
    self.oneButton.tag = 201;
    [self.view addSubview:self.oneButton];
    
    UILabel *oneLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.oneButton.frame.size.width, self.oneButton.frame.size.height/2)];
    [self.oneButton addSubview:oneLabel];
    oneLabel.font = [UIFont systemFontOfSize:15];
    oneLabel.textColor = ColorRGB(102, 102, 102);
    oneLabel.textAlignment = NSTextAlignmentCenter;
    oneLabel.text = [NSString stringWithFormat:@"%.2f元",[[self.dataSource[0] valueForKey:@"paid"] doubleValue]];
    self.rbmOne = oneLabel;
    
    UILabel *rmbLabelOne = [[UILabel alloc]initWithFrame:CGRectMake(0, oneLabel.frame.origin.y + oneLabel.frame.size.height, oneLabel.frame.size.width, oneLabel.frame.size.height)];
    rmbLabelOne.text = [NSString stringWithFormat:@"到账: %.2f星币",[[self.dataSource[0] valueForKey:@"coins"] doubleValue]];;
    rmbLabelOne.textColor = [UIColor whiteColor];
    rmbLabelOne.font = [UIFont systemFontOfSize:11];
    rmbLabelOne.textColor= ColorRGB(153, 153,153);
    rmbLabelOne.textAlignment = NSTextAlignmentCenter;
    self.starOne = rmbLabelOne;
    [self.oneButton addSubview:rmbLabelOne];
    
    
    
    
    
    
    
    self.threeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.threeButton.frame = CGRectMake(self.towButton.frame.origin.x + self.towButton.frame.size.width + 5,self.towButton.frame.origin.y,self.towButton.frame.size.width,self.towButton.frame.size.height);
    self.threeButton.tag = 203;
    self.threeButton.layer.cornerRadius = 5;
    self.threeButton.clipsToBounds = YES;
    self.threeButton.layer.borderColor = ColorRGB(153, 153, 153).CGColor;
    self.threeButton.layer.borderWidth = 1;
    self.threeButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.threeButton];
    
    
    UILabel *threeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.threeButton.frame.size.width, self.threeButton.frame.size.height/2)];
    [self.threeButton addSubview:threeLabel];
    threeLabel.textAlignment = NSTextAlignmentCenter;
    threeLabel.font = [UIFont systemFontOfSize:15];
    threeLabel.textColor = ColorRGB(102,102,102);
    threeLabel.text = [NSString stringWithFormat:@"%.2f元",[[self.dataSource[2] valueForKey:@"paid"] doubleValue]];
    self.rbmThree  = threeLabel;
    
    UILabel *rmbLabelThree = [[UILabel alloc]initWithFrame:CGRectMake(0, threeLabel.frame.origin.y + threeLabel.frame.size.height, threeLabel.frame.size.width, threeLabel.frame.size.height)];
    rmbLabelThree.text = [NSString stringWithFormat:@"到账: %.2f星币",[[self.dataSource[2] valueForKey:@"coins"] doubleValue]];
    rmbLabelThree.textColor = [UIColor whiteColor];
    rmbLabelThree.font = [UIFont systemFontOfSize:11];
    rmbLabelThree.textColor= ColorRGB(102,102,102);
    rmbLabelThree.textAlignment = NSTextAlignmentCenter;
    self.starThree = rmbLabelThree;
    [self.threeButton addSubview:rmbLabelThree];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    self.fiveButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.fiveButton.frame = CGRectMake(self.towButton.frame.origin.x,self.towButton.frame.origin.y + self.towButton.frame.size.height + 10, self.towButton.frame.size.width, 60);
    self.fiveButton.layer.borderColor = ColorRGB(153, 153, 153).CGColor;
    self.fiveButton.layer.borderWidth = 1;
    self.fiveButton.backgroundColor = [UIColor whiteColor];
    self.fiveButton.tag = 205;
    self.fiveButton.layer.cornerRadius = 5;
    self.fiveButton.clipsToBounds = YES;
    [self.view addSubview:self.fiveButton];
    
    UILabel *fiveLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.fiveButton.frame.size.width, self.fiveButton.frame.size.height/2)];
    [self.fiveButton addSubview:fiveLabel];
    fiveLabel.font = [UIFont systemFontOfSize:15];
    fiveLabel.textColor = ColorRGB(102,102,102);
    fiveLabel.textAlignment = NSTextAlignmentCenter;
    fiveLabel.text = [NSString stringWithFormat:@"%.2f元",[[self.dataSource[4] valueForKey:@"paid"] doubleValue]];
    self.rbmFive = fiveLabel;
    
    UILabel *rmbLabelFive = [[UILabel alloc]initWithFrame:CGRectMake(0, fiveLabel.frame.origin.y + fiveLabel.frame.size.height, fiveLabel.frame.size.width, fiveLabel.frame.size.height)];
    rmbLabelFive.text = [NSString stringWithFormat:@"到账: %.2f星币",[[self.dataSource[4] valueForKey:@"coins"] doubleValue]];;
    rmbLabelFive.font = [UIFont systemFontOfSize:11];
    rmbLabelFive.textColor= ColorRGB(153,153,153);
    rmbLabelFive.textAlignment = NSTextAlignmentCenter;
    self.starFive = rmbLabelFive;
    [self.fiveButton addSubview:rmbLabelFive];
    
    
    
    
    
    
    
    
    
    self.fourButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.fourButton.frame = CGRectMake(self.oneButton.frame.origin.x,self.fiveButton.frame.origin.y, self.oneButton.frame.size.width, 60);
    self.fourButton.layer.borderColor = ColorRGB(153, 153, 153).CGColor;
    self.fourButton.layer.borderWidth = 1;
    self.fourButton.backgroundColor = [UIColor whiteColor];
    self.fourButton.tag = 204;
    self.fourButton.layer.cornerRadius = 5;
    self.fourButton.clipsToBounds = YES;
    [self.view addSubview:self.fourButton];
    
    UILabel *fourLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.fourButton.frame.size.width, self.fourButton.frame.size.height/2)];
    [self.fourButton addSubview:fourLabel];
    fourLabel.font = [UIFont systemFontOfSize:15];
    fourLabel.textColor = ColorRGB(102,102,102);
    fourLabel.textAlignment = NSTextAlignmentCenter;
    self.rbmFour = fourLabel;
    fourLabel.text = [NSString stringWithFormat:@"%.2f元",[[self.dataSource[3] valueForKey:@"paid"] doubleValue]];
    
    UILabel *rmbLabelFour = [[UILabel alloc]initWithFrame:CGRectMake(0, fourLabel.frame.origin.y + fourLabel.frame.size.height, fourLabel.frame.size.width, fourLabel.frame.size.height)];
    rmbLabelFour.text = [NSString stringWithFormat:@"到账: %.2f星币",[[self.dataSource[3] valueForKey:@"coins"] doubleValue]];
    rmbLabelFour.font = [UIFont systemFontOfSize:11];
    rmbLabelFour.textColor= ColorRGB(153,153,153);
    rmbLabelFour.textAlignment = NSTextAlignmentCenter;
    self.starFour = rmbLabelFour;
    [self.fourButton addSubview:rmbLabelFour];
    
    
    
    
    
    
    self.sixButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.sixButton.frame = CGRectMake(self.towButton.frame.origin.x + self.towButton.frame.size.width + 5,self.fiveButton.frame.origin.y,self.towButton.frame.size.width,self.towButton.frame.size.height);
    self.sixButton.layer.borderColor = ColorRGB(153, 153, 153).CGColor;
    self.sixButton.layer.borderWidth = 1;
    self.sixButton.backgroundColor = [UIColor whiteColor];
    self.sixButton.layer.cornerRadius = 5;
    self.sixButton.clipsToBounds = YES;
    self.sixButton.tag = 206;
    [self.view addSubview:self.sixButton];
    
    UILabel *sixLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.sixButton.frame.size.width, self.sixButton.frame.size.height/2)];
    [self.sixButton addSubview:sixLabel];
    sixLabel.font = [UIFont systemFontOfSize:15];
    sixLabel.textColor = ColorRGB(102,102,102);
    
    sixLabel.textAlignment = NSTextAlignmentCenter;
    sixLabel.text = [NSString stringWithFormat:@"%.2f元",[[self.dataSource[5] valueForKey:@"paid"] doubleValue]];
    self.rbmSix = sixLabel;
    
    UILabel *rmbLabelSix = [[UILabel alloc]initWithFrame:CGRectMake(0, sixLabel.frame.origin.y + sixLabel.frame.size.height, sixLabel.frame.size.width, sixLabel.frame.size.height)];
    rmbLabelSix.text = [NSString stringWithFormat:@"到账: %.2f星币",[[self.dataSource[5] valueForKey:@"coins"]  doubleValue]];
    rmbLabelSix.font = [UIFont systemFontOfSize:11];
    rmbLabelSix.textColor= ColorRGB(153,153,153);
    rmbLabelSix.textAlignment = NSTextAlignmentCenter;
    self.starSix = rmbLabelSix;
    [self.sixButton addSubview:rmbLabelSix];
    self.sevenbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.sevenbutton.frame = CGRectMake(self.fourButton.frame.origin.x, self.fourButton.frame.origin.y + self.fourButton.frame.size.height + 10, self.fourButton.frame.size.width, self.fourButton.frame.size.height);
    self.sevenbutton.layer.borderColor = ColorRGB(153, 153, 153).CGColor;
    self.sevenbutton.layer.borderWidth = 1;
    self.sevenbutton.backgroundColor = [UIColor whiteColor];
    [self.sevenbutton setTitle:@"其他金额" forState:(UIControlStateNormal)];
    [self.sevenbutton setTitleColor:ColorRGB(153, 153, 153) forState:(UIControlStateNormal)];
    self.sevenbutton.layer.cornerRadius = 5;
    self.sevenbutton.tag = 207;
    self.sevenbutton.clipsToBounds = YES;
    [self.view addSubview:self.sevenbutton];
    
    [self.oneButton addTarget:self action:@selector(selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.towButton addTarget:self action:@selector(selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.threeButton addTarget:self action:@selector(selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.fourButton addTarget:self action:@selector(selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.fiveButton addTarget:self action:@selector(selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.sixButton addTarget:self action:@selector(selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.sevenbutton addTarget:self action:@selector(selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    
    self.allTowView = [[UIView alloc]initWithFrame:CGRectMake(self.fiveButton.frame.origin.x, self.sevenbutton.frame.origin.y,self.sevenbutton.frame.size.width *2 + 5 , self.sevenbutton.frame.size.height)];
    self.allTowView.layer.cornerRadius = 5;
    self.allTowView.clipsToBounds = YES;
    self.allTowView.layer.borderColor = ColorRGB(102, 204, 255).CGColor;
    self.allTowView.layer.borderWidth = 1;
    
    [self.view addSubview:self.allTowView];
    
    
    self.sevenField = [[UITextField alloc]initWithFrame:CGRectMake(5, 10, self.sevenbutton.frame.size.width, 40)];
    self.sevenField.placeholder = @"请输入金额";
    self.sevenField.font = [UIFont systemFontOfSize:13];
    self.sevenField.borderStyle = UITextBorderStyleRoundedRect;
    self.sevenField.textAlignment = NSTextAlignmentCenter;
    self.sevenField.keyboardType = UIKeyboardTypeNumberPad;
    self.sevenField.delegate = self;
    
    [self.allTowView addSubview:self.sevenField];
    self.allTowView.hidden = YES;
    
    
    self.eightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.eightButton setTitle:@"立即充值" forState:(UIControlStateNormal)];
    [self.eightButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.eightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.eightButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.eightButton.backgroundColor = ColorRGB(102, 204, 255);
    
    
    self.eightButton.frame = CGRectMake(self.sevenField.frame.origin.x + self.sevenField.frame.size.width + 5, 10, self.sevenField.frame.size.width-10, 40);
    self.eightButton.layer.cornerRadius = 5;
    self.eightButton.clipsToBounds = YES;
    [self.allTowView addSubview:self.eightButton];
    
    
    [self.eightButton addTarget:self action:@selector(eightButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
    
    
#pragma mark ----eightButton方法的实现
- (void)eightButtonAction{
    [self.sevenField resignFirstResponder];
    
    if ([self.sevenField.text integerValue] <10) {
        
        [self createAlertViewWithTitle:nil message:@"请输入10元或10元以上的金额" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
    }else{
        
        [self buttonAction];
        
    }
    
    
    
}
    
    
    
    
    
- (void)selectAction:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    
    [self.sevenField resignFirstResponder];
    
    if (201 == tag) {
        self.typeStr = @"2";
        self.sumPrice = [NSString stringWithFormat:@"%@",[self.dataSource[0] valueForKey:@"paid"]];
        self.starSum = [NSString stringWithFormat:@"%@",[self.dataSource[0] valueForKey:@"coins"]];
        self.couponId = [NSString stringWithFormat:@"%@",[self.dataSource[0] valueForKey:@"id"]];
        
        self.allTowView.hidden = YES;
        [self buttonAction];
        [self oneButton:self.oneButton towButton:self.towButton threeButton:self.threeButton fourButton:self.fourButton fiveButton:self.fiveButton sixButton:self.sixButton rmbOne:self.rbmOne rmbTow:self.rbmTow rmbThree:self.rbmThree rmbFour:self.rbmFour rmbFive:self.rbmFive rmbSix:self.rbmSix starOne:self.starOne starTow:self.starTow starThree:self.starThree starFour:self.starFour starFive:self.starFive starSix:self.starSix sevenButton:self.sevenbutton rbmSeven:self.rbmTow starSeven:self.starTow];
        
        
    }else if (202 == tag){
        self.typeStr = @"2";
        self.sumPrice = [NSString stringWithFormat:@"%@",[self.dataSource[1] valueForKey:@"paid"]];
        self.starSum = [NSString stringWithFormat:@"%@",[self.dataSource[1] valueForKey:@"coins"]];
        self.couponId = [NSString stringWithFormat:@"%@",[self.dataSource[1] valueForKey:@"id"]];
        
        self.allTowView.hidden = YES;
        [self buttonAction];
        [self oneButton:self.towButton towButton:self.oneButton threeButton:self.threeButton fourButton:self.fourButton fiveButton:self.fiveButton sixButton:self.sixButton rmbOne:self.rbmTow rmbTow:self.rbmOne rmbThree:self.rbmThree rmbFour:self.rbmFour rmbFive:self.rbmFive rmbSix:self.rbmSix starOne:self.starTow starTow:self.starOne starThree:self.starThree starFour:self.starFour starFive:self.starFive starSix:self.starSix sevenButton:self.sevenbutton rbmSeven:self.rbmSix starSeven:self.starSix];
        
    }else if (203 == tag){
        self.typeStr = @"2";
        self.sumPrice = [NSString stringWithFormat:@"%@",[self.dataSource[2] valueForKey:@"paid"]];
        self.starSum = [NSString stringWithFormat:@"%@",[self.dataSource[2] valueForKey:@"coins"]];
        self.couponId = [NSString stringWithFormat:@"%@",[self.dataSource[2] valueForKey:@"id"]];
        
        self.allTowView.hidden = YES;
        [self buttonAction];
        [self oneButton:self.threeButton towButton:self.towButton threeButton:self.oneButton fourButton:self.fourButton fiveButton:self.fiveButton sixButton:self.sixButton rmbOne:self.rbmThree rmbTow:self.rbmTow rmbThree:self.rbmOne rmbFour:self.rbmFour rmbFive:self.rbmFive rmbSix:self.rbmSix starOne:self.starThree starTow:self.starTow starThree:self.starOne starFour:self.starFour starFive:self.starFive starSix:self.starSix  sevenButton:self.sevenbutton rbmSeven:self.rbmSix starSeven:self.starSix];
        
    }else if (204 == tag){
        self.typeStr = @"2";
        self.sumPrice = [NSString stringWithFormat:@"%@",[self.dataSource[3] valueForKey:@"paid"]];
        self.starSum = [NSString stringWithFormat:@"%@",[self.dataSource[3] valueForKey:@"coins"]];
        self.couponId = [NSString stringWithFormat:@"%@",[self.dataSource[3] valueForKey:@"id"]];
        
        self.allTowView.hidden = YES;
        [self buttonAction];
        [self oneButton:self.fourButton towButton:self.towButton threeButton:self.threeButton fourButton:self.oneButton fiveButton:self.fiveButton sixButton:self.sixButton rmbOne:self.rbmFour rmbTow:self.rbmTow rmbThree:self.rbmThree rmbFour:self.rbmOne rmbFive:self.rbmFive rmbSix:self.rbmSix starOne:self.starFour starTow:self.starTow starThree:self.starThree starFour:self.starOne starFive:self.starFive starSix:self.starSix sevenButton:self.sevenbutton rbmSeven:self.rbmSix starSeven:self.starSix];
        
    }else if (205 == tag){
        self.typeStr = @"2";
        self.sumPrice = [NSString stringWithFormat:@"%@",[self.dataSource[4] valueForKey:@"paid"]];
        self.starSum = [NSString stringWithFormat:@"%@",[self.dataSource[4] valueForKey:@"coins"]];
        self.couponId = [NSString stringWithFormat:@"%@",[self.dataSource[4] valueForKey:@"id"]];
        
        self.allTowView.hidden = YES;
        [self buttonAction];
        [self oneButton:self.fiveButton towButton:self.towButton threeButton:self.threeButton fourButton:self.fourButton fiveButton:self.oneButton sixButton:self.sixButton rmbOne:self.rbmFive rmbTow:self.rbmTow rmbThree:self.rbmThree rmbFour:self.rbmFour rmbFive:self.rbmOne rmbSix:self.rbmSix starOne:self.starFive starTow:self.starTow starThree:self.starThree starFour:self.starFour starFive:self.starOne starSix:self.starSix sevenButton:self.sevenbutton rbmSeven:self.rbmSix starSeven:self.starSix];
        
    }else if (206 == tag){
        
        self.typeStr = @"2";
        self.sumPrice = [NSString stringWithFormat:@"%@",[self.dataSource[5] valueForKey:@"paid"]];
        self.starSum = [NSString stringWithFormat:@"%@",[self.dataSource[5] valueForKey:@"coins"]];
        self.couponId = [NSString stringWithFormat:@"%@",[self.dataSource[5] valueForKey:@"id"]];
        
        self.allTowView.hidden = YES;
        [self buttonAction];
        [self oneButton:self.sixButton towButton:self.towButton threeButton:self.threeButton fourButton:self.fourButton fiveButton:self.fiveButton sixButton:self.oneButton rmbOne:self.rbmSix rmbTow:self.rbmTow rmbThree:self.rbmThree rmbFour:self.rbmFour rmbFive:self.rbmFive rmbSix:self.rbmOne starOne:self.starSix starTow:self.starTow starThree:self.starThree starFour:self.starFour starFive:self.starFive starSix:self.starOne sevenButton:self.sevenbutton rbmSeven:self.rbmTow starSeven:self.starTow];
        
    }else if (207 == tag){
        
        self.allTowView.hidden = NO;
        self.sevenField.text = nil;
        self.couponId = @"0";
        self.starSum  =  @"0";
        [self oneButton:self.sevenbutton towButton:self.towButton threeButton:self.threeButton fourButton:self.fourButton fiveButton:self.fiveButton sixButton:self.oneButton rmbOne:self.rbmSix rmbTow:self.rbmTow rmbThree:self.rbmThree rmbFour:self.rbmFour rmbFive:self.rbmFive rmbSix:self.rbmOne starOne:self.starSix starTow:self.starTow starThree:self.starThree starFour:self.starFour starFive:self.starFive starSix:self.starOne sevenButton:self.self.sixButton rbmSeven:self.rbmTow starSeven:self.starTow];
        self.rbmSix.textColor = ColorRGB(102, 102, 102);
        self.starSix.textColor = ColorRGB(153, 153, 153);
        
        
    }
    
    
    
}
    
- (void)oneButton:(UIButton *)oneButton towButton:(UIButton *)towButton threeButton:(UIButton *)threeButton fourButton:(UIButton *)fourButton fiveButton:(UIButton *)fiveButton sixButton:(UIButton *)sixButton rmbOne:(UILabel *)rmbOne rmbTow:(UILabel *)rmbTow rmbThree:(UILabel *)rmbThree rmbFour:(UILabel *)rmbFour rmbFive:(UILabel *)rmbFive rmbSix:(UILabel *)rmbSix  starOne:(UILabel *)starOne starTow:(UILabel *)starTow starThree:(UILabel *)starThree starFour:(UILabel *)starFour  starFive:(UILabel *)starFive  starSix:(UILabel *)starSix  sevenButton:(UIButton *)sevenButton  rbmSeven:(UILabel *)rbmSeven starSeven:(UILabel *)starSeven{
    
    oneButton.layer.borderColor = ColorRGB(102, 204, 255).CGColor;
    oneButton.layer.borderWidth = 1;
    towButton.layer.borderColor = ColorRGB(204, 204, 204).CGColor;
    towButton.layer.borderWidth = 1;
    threeButton.layer.borderColor = ColorRGB(204, 204, 204).CGColor;
    threeButton.layer.borderWidth = 1;
    fourButton.layer.borderColor = ColorRGB(204, 204, 204).CGColor;
    fourButton.layer.borderWidth = 1;
    fiveButton.layer.borderColor = ColorRGB(204, 204,204).CGColor;
    fiveButton.layer.borderWidth = 1;
    sixButton.layer.borderColor = ColorRGB(204, 204, 204).CGColor;
    sixButton.layer.borderWidth = 1;
    sevenButton.layer.borderColor = ColorRGB(204, 204, 204).CGColor;
    sevenButton.layer.borderWidth = 1;
    
    
    rmbOne.textColor = ColorRGB(102, 204, 255);
    rmbTow.textColor = ColorRGB(102, 102, 102);
    rmbThree.textColor = ColorRGB(102, 102, 102);
    rmbFour.textColor = ColorRGB(102, 102, 102);
    rmbFive.textColor = ColorRGB(102, 102, 102);
    rmbSix.textColor = ColorRGB(102, 102, 102);
    rbmSeven.textColor = ColorRGB(102, 102, 102);
    
    starOne.textColor = ColorRGB(141, 216, 254);
    starTow.textColor = ColorRGB(153, 153, 153);
    starThree.textColor = ColorRGB(153, 153, 153);
    starFour.textColor = ColorRGB(153, 153, 153);
    starFive.textColor = ColorRGB(153, 153, 153);
    starSix.textColor = ColorRGB(153, 153, 153);
    starSeven.textColor = ColorRGB(153, 153, 153);
    
    
    
    
    
    
    
    
    
}
    
    
    
    
    
#pragma mark  ----充值记录方法的实现
- (void)rightItemAction{
    
    RechargeRecordVC *VC = [[RechargeRecordVC alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}
    
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    return YES;
}
    
    
    
    
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.typeStr = @"1";
    self.sumPrice = textField.text;
    double  sum =    [self.rechargeRate doubleValue] *[textField.text doubleValue];
    
    self.starSum  = [NSString stringWithFormat:@"%ld",(long)sum];
    [self oneButton:self.sevenbutton towButton:self.towButton threeButton:self.threeButton fourButton:self.fourButton fiveButton:self.fiveButton sixButton:self.sixButton rmbOne:self.rbmSix rmbTow:self.rbmTow rmbThree:self.rbmThree rmbFour:self.rbmFour rmbFive:self.rbmFive rmbSix:self.rbmOne starOne:self.starSix starTow:self.starTow starThree:self.starThree starFour:self.starFour starFive:self.starFive starSix:self.starOne sevenButton:self.oneButton rbmSeven:self.rbmSix starSeven:self.starSix];
}
    
    
    
    
    
#pragma mark ----- 充值方法的实现
- (void)buttonAction{
    
    if (self.sumPrice.length == 0) {
        
        [self createAlertViewWithTitle:nil message:@"请选择充值金额" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        
    }else{
        
        
        
        
        
        
        
        
        
        UIWindow *window = [[UIWindow alloc] init];
        window.frame = CGRectMake(0.f, 0.f, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height);
        window.backgroundColor = HUDBackgroundColor;
        window.windowLevel = UIWindowLevelNormal;
        window.alpha = 1.f;
        window.hidden = NO;
        
        ZYStarchongView *payView = [[ZYStarchongView alloc]init];
        payView.size = CGSizeMake(kScreenWidth - 60, 280 * kScreenHeight/667 + 100);
        payView.price = [self.sumPrice doubleValue];
        payView.withholdLabel_1.text = [NSString stringWithFormat:@"到账星币数：%.2f",[self.starSum doubleValue]];
        payView.center = window.center;
        [window addSubview:payView];
        UITapGestureRecognizer *windowTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(windowTapped)];
        [window addGestureRecognizer:windowTap];
        
        _seenWindow = window;
        payView.wxSelectButtonClickedBlock = ^(UIButton *button,NSInteger tag){
            
            
        };
        
        //支付宝支付
        payView.zfbSelectButtonClickedBlock = ^(UIButton *button,NSInteger tag){
            
        };
        
        
        //付款
        
        payView.payButtonClickedBlock = ^(UIButton *button, NSInteger tag) {
            
            if (201 == tag) {
               // [self initNetworkingOfWeChat];
            }else if (202 == tag){
                
               // [self initNetworkingOfAliPay];
                
            }
            
        };
        
    }
}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
#pragma mark ----- 支付宝支付方法的实现
- (void)initNetworkingOfAliPay{
    
    [self windowTapped];
    
    
    
    
    
    NSString *url = [NSString stringWithFormat:@"aliChargeOrderInfo.do?total_fee=%@&type=%@&couponId=%@",self.sumPrice,self.typeStr,self.couponId];
    
    
    NSString *changeUilStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [MBProgressHUD showHUD];
    
    NSLog(@"%@ %@",kMongyPay(changeUilStr),kMongyPay(url));
    
    
    [NetManager Get:kMongyPay(changeUilStr) Auth:YES Params:nil Success:^(id Obj) {
        [MBProgressHUD hideHUD];
        
        
        NSString *str = [Obj valueForKey:@"t"];
        NSString *appScheme = @"guanlixitong";
        [[AlipaySDK defaultService] payOrder:str fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            
            
            
            
            NSString *str = [resultDic valueForKey:@"result"];
            
            NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
            
            NSError *err;
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                 
                                                                options:NSJSONReadingMutableContainers
                                 
                                                                  error:&err];
            
            
            NSDictionary *dict =[dic valueForKey:@"alipay_trade_app_pay_response"];
            
            
            if ([[dict valueForKey:@"code"]integerValue]  ==10000) {
                
                [MBProgressHUD showSuccess:@"支付成功"];
                
                NSString *str = [self.sumLabel.text stringByReplacingOccurrencesOfString:@"余额：" withString:@""];
                str =  [str stringByReplacingOccurrencesOfString:@"星币" withString:@""];
                double coins = [str doubleValue];
                coins += [self.sumPrice  doubleValue];
                self.sumLabel.text = [NSString stringWithFormat:@"余额：%.2f星币",coins];
                
            }else{
                
                [MBProgressHUD showSuccess:@"支付失败"];
                
            }
            
        }];
    } Fail:^(NSError *Error) {
          [MBProgressHUD showError:@"预付订单生成失败"];
    }];
    
    
   
    
}
    
#pragma mark --- 微信支付方法的实现
-(void)initNetworkingOfWeChat{
    
    //[payFee=10|coins=0|packageId=48|selection=242-1,230-11,214-2]
    
    
    
    NSInteger payFee = [self.sumPrice doubleValue] *100;   // 单位分
    
    NSString *url = [NSString stringWithFormat:@"wxChargePayInfo.do?payFee=%ld&type=%@&couponId=%@",(long)payFee,self.typeStr,self.couponId
                     ];
    
    NSString *authCode = [AccountTool shareAccountTool].currentAccount.authCode;
    
    
    
    NSLog(@"%@ %@",kMongyPay(url),authCode);
    
    [NetManager Get:kMongyPay(url) Auth:YES Params:nil Success:^(id Obj) {
        
        if([[[Obj objectForKey:@"code"] stringValue]isEqualToString:@"1000"]){
            
            NSDictionary *dict = [Obj objectForKey:@"t"];
            
            //预付单号
            _prepayid = [dict objectForKey:@"prepayid"];
            _noncestr = [dict objectForKey:@"noncestr"];
            _sign = [dict objectForKey:@"sign"];
            _timestamp = [dict objectForKey:@"timestamp"];
            
            if (_prepayid.length != 0) {
                
                
                //跳转微信支付页面
                [self WXPay];
            }else{
                
                [MBProgressHUD showError:@"预付订单生成失败"];
            }
            
            
        }else{
            
            [MBProgressHUD showError:[Obj objectForKey:@"msg"]];
        }
        
    } Fail:^(NSError *Error) {
         [MBProgressHUD showError:@"预付订单生成失败"];
    }];
    
    
   
    
    
    
    
    
    
    
    
}
    
    
    
#pragma mark - 微信
    /**
     *  跳转微信支付页面
     */
- (void)WXPay{
    if (![WXApi isWXAppInstalled]) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未安装微信" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    // 获取当前时间
    time_t now;
    time(&now);
    
    // 时间戳
    //NSString *timestamp = [NSString stringWithFormat:@"%ld", now];
    
    // 随机字符串
    //NSString *nonceStr = [[self md5:timestamp] uppercaseString];
    
    //订单id
    NSString *prepayid =   _prepayid;
    
    /*  服务器生成签名
     //生成签名
     NSDictionary *para = @{
     @"appid" : WXAppID,
     @"noncestr" : nonceStr,
     @"package" : @"Sign=WXPay",
     @"partnerid" : WXPartnerid,
     @"prepayid" : prepayid,
     @"timestamp" : timestamp
     };
     
     NSMutableString *contentString = [NSMutableString string];
     NSArray *keys = [para allKeys];
     // 按字母排序
     NSArray *sortedArry = [keys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
     return [obj1 compare:obj2 options:NSNumericSearch];
     }];
     
     // 拼接字符串
     for (NSString *categoryId  in sortedArry) {
     
     if (![[para objectForKey:categoryId] isEqualToString:@""] && ![categoryId isEqualToString:@"sign"] && ![categoryId isEqualToString:@"key"]) {
     
     [contentString appendFormat:@"%@=%@&", categoryId, para[categoryId]];
     }
     }
     // 添加key字段
     [contentString appendFormat:@"key=%@", apiKey];
     // 加密生成字符串
     NSString *sign = [self md5:contentString];
     */
    
    
    PayReq *request = [[PayReq alloc] init];
    /** 商家向财付通申请的商家id */
    request.partnerId = WXPartnerid;
    /** 预支付订单 */
    request.prepayId= prepayid;
    /** 商家根据财付通文档填写的数据和签名 */
    request.package = @"Sign=WXPay";
    /** 随机串，防重发 */
    request.nonceStr= _noncestr;
    /** 时间戳，防重发 */
    request.timeStamp = [_timestamp intValue];
    /** 商家根据微信开放平台文档对数据做的签名 */
    request.sign= _sign;
    /*! @brief 发送请求到微信，等待微信返回onResp
     *
     * 函数调用后，会切换到微信的界面。第三方应用程序等待微信返回onResp。微信在异步处理完成后一定会调用onResp。支持以下类型
     * SendAuthReq、SendMessageToWXReq、PayReq等。
     * @param req 具体的发送请求，在调用函数后，请自己释放。
     * @return 成功返回YES，失败返回NO。
     */
    [WXApi sendReq: request];
    
}
    
    
-(void)WXCallback:(NSNotification *)sender{
    
    int code = [[sender.userInfo objectForKey:@"errCode"] intValue];;
    
    if (code == 0) {
        
        
        [MBProgressHUD showSuccess:@"支付成功"];
        
        
        
    }else{
        
        [MBProgressHUD showError:@"支付失败"];
    }
    
    [self removeWindowAction];
    
    
}
    
    
    
#pragma mark - 支付依赖的方法
-(void)windowTapped{
    
    [self removeWindowAction];
}
    
-(void)removeWindowAction{
    
    _seenWindow.hidden = YES;
    
    _seenWindow = nil;
    
}
    
    @end
